
<script type="text/javascript">
$(function(){
    $.get('/admin/getcategories',{},function(resp){
        $('.segment').find('.category').html(resp).css({'font-size':'10px',width:'auto',margin:0});
        var opt = $('.category').find('select option');
        $('.category').find('select').change(setCategory);
        opt.each(function(i,op){
            var data = $(op).parents('.category').attr('data-value');
            if(data == $(op).val()){
                $(op).attr('selected','selected');
            }
        });
    });
    var pagesCount = $('#pages').data('artcount');
    var pages = $('#pages');
    var title = $('.title');
    var newtitle = [];
    for(var i = 0;i < title.length;i++){
         if($(title[i]).text().length > 60){
             var txt =$(title[i]).text();
             newtitle[i] = txt.substring(0,60)+'...';
         }
    }
    for(var i = 0;i < title.length;i++){
        $(title[i]).text(newtitle[i]);
    }
    if(pagesCount > 10){
        pages.tabPagination();
    }else{
        pages.hide();
    }
    $('.score').jRating({isDisabled:true,type:'small',rateMax:5});
    var visib = $('.visibility');
    var cat = $('.categories');
    $(visib).change(function(){visibility($(this));});
    cat.change(setCategory);

    var comments = $('.comments');
    comments.each(function(i,el){
        var elm = $(el);
        if(elm.text() > '0'){
            elm.attr('title','Просмотреть комментарии к статье').css({cursor:'pointer','text-decoration':'underline','font-weight':'bold'});
            elm.bind('click',getComments);

        }else{
            elm.css({cursor:'default','text-decoration':'none'});
            var img = $('<img>',{id:'imgcomment',src:'/images/nocomment.png',style:'vertical-align:middle;padding-right:3px;'}).attr('title','Комментариев нет');
            elm.parent().find('img').replaceWith(img);
        }
    });

//    console.log( $('*').length );

});
function getComments(){

    $(this).comments();
}

function setCategory(){

    var p = $.post('/admin/setcategories',{id:$(this).parent().attr('id'),option:$(this).val()});

}
function getCategories(elem){

    var sel,opt;
    var par = $(elem).parent().attr('id');
    var selected = $(elem).val();
    $.get('/admin/getcategories',{},function(resp){
        $(elem).parent().find('.category').append(resp);
    });
}
function visibility(vis){
    var data = {};
    $.each(vis,function(i,elm){
            data.id = $(vis).attr('id');
            data.value = $(vis).val();
            $.ajax({
                url:'/admin/site/setvisib',
                dataType:'JSON',
                type:'POST',
                data:data
            });
    });
}
function returnBack(){
    $('#comment-container').remove();
    $('#backward').remove();
    $('#tab').show();
}
</script>

<?php if(isset($this->page[0])):?>
<div class="container" >
<?php foreach($this->page as $p):?>
    <?php if(isset($p)):?>
<div id="<?=$this->escape($p['article_id'])?>" class="segment" style="border-bottom:1px solid #555;padding:5px 0;">
    <div  id="image" style="padding:0 10px;word-wrap:break-word;"><img src="/images/articles/<?=$this->escape($p['img'])?>"  style="height:40px;vertical-align:middle;border-radius:3px;"/><span class="title" style="padding:10px;font-weight:bold;"><?=$this->escape($p['title'])?></span></div>

    <span class="tabtext"><img src="/images/calendar.png" style="vertical-align:middle;padding:0 5px;" title="Дата создания"></span><span class="created"><?=$this->escape($p['created_dt']);?></span>
    | <span class="tabtext">Категория:</span><span id="<?=$this->escape($p['article_id'])?>" style="width:50px;" class="category" data-value="<?=$this->escape($p['category_name'])?>">

    </span>
    | <span class="tabtext">Опубликован:</span>
    <select class="visibility" id="<?=$this->escape($p['article_id'])?>" style="font-size:10px;">
       <option value="<?=$this->escape($p['visibility'])=='yes' ? 'yes':'no'?>"><?=$this->escape($p['visibility'])=='yes' ? 'да':'нет'?></option>
       <option value="<?=$this->escape($p['visibility'])=='yes' ? 'no' : 'yes' ?>"><?=$this->escape($p['visibility'])=='yes' ? 'нет' : 'да' ?></option>
    </select>
    | <span class="tabtext">Просмотров:</span><span id="article_<?=$this->escape($p['article_id'])?>" class="views"><?=$this->escape($p['views']) == null ? 0 :$this->escape($p['views']);?></span>
    | <span class="comm"><img src="/images/comment.png" id="imgcomment" style="vertical-align:middle;padding-right:3px;" title="Комметариев к статье"><span class="tabtext"></span><span id="<?=$this->escape($p['article_id'])?>" class="comments"><?=$this->escape($p['commcount']) == false ? 0 : $this->escape($p['commcount']);?></span></span>
    | <span class="tabtext">Всего голосов:</span><span id="count" class="countvotes"><?=$this->escape($p['countvote']) == null ? 0: $this->escape($p['countvote']); ?></span> |
     <span class="tabtext">Рейтинг:</span><span id="rating_<?=$this->escape($p['article_id'])?>" class="rating" style="padding:0 5px;margin:0;"><?=$this->escape($p['sumscore']) !=null ? $this->escape($p['sumscore']) :0 ?></span><span id="score_<?=$this->escape($p['article_id'])?>" class="score" data-average="<?=$this->escape($p['sumscore']) !=null ? $this->escape($p['sumscore']) :0?>" style="float:right;margin:6px 0px 0 5px;"></span>
</div>
    <?php else:?>
    <?php endif;?>
<?php endforeach;?>
</div>

<?php else:?>
    <p style="width:850px;text-align:center;"><strong>Статей не обнаружено.</strong></p>
<?php endif;?>
<div id="progressbar"></div>
<div id="pages"  class="tab_bottom" data-pagescount="<?=$this->escape($this->pages)?>" data-artcount="<?=$this->escape($this->artcount)?>">
</div>

